<?php
namespace Admin\Controller;
use Think\Controller;

class MindexController extends Controller
{
	//轮播图
	public function LunBoTu()
	{
		$lb = M('lbt');
		$data = $lb->select();
		$this->assign('list',$data);
		$this->display('Mindex/LunBoTu');
	}

	public function LBTDadd()
	{
		
		$config = array(
			'maxSize' => 3145728,
			'rootPath' => './Public/',
    		'savePath' => '/Home/image/LunBoTu/',
			'saveName' => array('uniqid',''),
			'autoSub' => false,
			'exts' => array('jpg', 'gif', 'png', 'jpeg'),
		);
		$upload = new \Think\Upload($config);//  实例化上传类
		
		$info = $upload->uploadOne($_FILES['photo']);
		
		if(!$info) {//  上传错误提示错误信息
			$this->error($upload->getError());
		}else{//  上传成功 获取上传文件信息
			$data['image'] = $info['savename'];
			$data['href'] = $_POST['href'];
			$data['alt'] = $_POST['alt'];
			M('lbt')->data($data)->add();
			$this->success("添加成功!",U('Mindex/LunBoTu'));
		}
	
	}
	
	public function LBTedit()
	{
		$lb = M('lbt');
		$map['lid'] = array('eq',I('get.lid'));
		$data = $lb->where($map)->find();
		$this->assign('list',$data);
		$this->display('Mindex/LBTedit');
	}

	public function LBTupdate()
	{
		if (empty($_POST)) {
			$this->error('请完善数据!');
			exit;
		}
		//上传新图片
		$config = array(
			'maxSize' => 3145728,
			'rootPath' => './Public/',
    		'savePath' => '/Home/image/LunBoTu/',
			'saveName' => array('uniqid',''),
			'autoSub' => false,
			'exts' => array('jpg', 'gif', 'png', 'jpeg'),
		);
		$upload = new \Think\Upload($config);//  实例化上传类
		
		$info = $upload->uploadOne($_FILES['photo']);
		//拼接原来图片路径
		$path = './Public/Home/image/LunBoTu/'.$_POST['image'];
		
		if(!$info) {//  上传错误提示错误信息
			$this->error($upload->getError());
		}else{//  上传成功 获取上传文件信息
			$data['image'] = $info['savename'];
			$data['href'] = $_POST['href'];
			$data['alt'] = $_POST['alt'];
			$data['lid'] = $_POST['lid'];
			M('lbt')->create($data);
			if (M('lbt')->save() >0) {
				unlink($path);
				$this->success('编辑成功',U('Mindex/LunBoTu'));
			} else {
				$this->error('编辑失败');
			}
		}

		
	}

	public function LBTdel()
	{
		//判断 有无传递ID
		if (empty($_GET['lid'])) {
			$this->redirect('Mindex/LunBoTu');
			exit;
		}
		//接收ID
		$id = I('get.lid/d');
		//接收拼接原图片路径
		$path = './Public/Home/image/LunBoTu/'.I('get.image');

		if (M('lbt')->delete($id) >0) {
			unlink($path);
			$this->success('恭喜你,删除成功',U('Mindex/LunBoTu'));
		} else {
			$this->error('删除失败',U('Mindex/LunBoTu'));
		}
	}

	//文件上传 显示界面
	public function FileIndex()
	{
		$file = M('file');
		$data = $file->select();
		$this->assign('data',$data);
		$this->display();
	}

	//文件上传 添加
	public function FileAdd()
	{
		
		$config = array(
			'maxSize' => 3145728,
			'rootPath' => './Public/',
    		'savePath' => '/Home/Download/',
			'saveName' => array('uniqid',''),
			'autoSub' => false,
			'exts' => array('rar', 'txt', 'xlsx','xls', 'doc', 'docx'),
		);
		$upload = new \Think\Upload($config);//  实例化上传类
		
		$info = $upload->uploadOne($_FILES['file']);
		if(!$info) {//  上传错误提示错误信息
			$this->error($upload->getError());
		}else{//  上传成功 获取上传文件信息
			$data['path'] = $info['savename'];
			$data['filename'] = $_POST['filename'];
			M('file')->data($data)->add();
			$this->success('编辑成功',U('Mindex/FileIndex'));
		}
	
	}
	//文件编辑
	public function FileUpdate()
	{

		if (empty($_POST)) {
			$this->error('请完善数据!');
			exit;
		}
		//上传新文件
		$config = array(
			'maxSize' => 3145728,
			'rootPath' => './Public/',
    		'savePath' => '/Home/Download/',
			'saveName' => array('uniqid',''),
			'autoSub' => false,
			'exts' => array('rar', 'txt', 'xlsx','xls', 'doc', 'docx'),
		);
		$upload = new \Think\Upload($config);//  实例化上传类
		
		$info = $upload->uploadOne($_FILES['file']);
		//拼接原来图片路径
		$path = './Public/Home/Download/'.$_POST['path'];
		
		if(!$info) {//  上传错误提示错误信息
			$this->error($upload->getError());
		}else{//  上传成功 获取上传文件信息
			$data['path'] = $info['savename'];
			$data['filename'] = $_POST['filename'];
			$data['fileid'] = $_POST['fileid'];
			M('file')->create($data);
			if (M('file')->save() >0) {
				unlink($path);
				$this->success('编辑成功',U('Mindex/FileIndex'));
			} else {
				$this->error('编辑失败');
			}
		}
	}


	public function FileDel()
	{

		//判断 有无传递ID
		if (empty($_GET['fileid'])) {
			$this->redirect('Mindex/FileIndex');
			exit;
		}
		//接收ID
		$id = I('get.fileid/d');
		//接收拼接原文件路径
		$path = './Public/Home/Download/'.I('get.path');

		if (M('file')->delete($id) >0) {
			unlink($path);
			$this->ajaxReturn('1');
		} else {
			$this->ajaxReturn('0');
		}
	}























}